package controllers;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import models.User;
import play.db.Model;
import play.mvc.Controller;
import play.mvc.With;
import services.PointService;
import services.UserService;

@With(Secure.class)
public class Point extends Controller {
	
	@Inject
    private static PointService pointService;
	@Inject
	private static UserService userService;
	
	public static void index(int id) {
        render(id);
    }
	
	private static models.Point getParams(){
		String posid= params.get("posid");
		String pid 	= params.get("pid");
        String type = params.get("type");
        String lat 	= params.get("lat");
        String lng 	= params.get("lng");
        
        User user = userService.getUserByUsername(Secure.Security.connected());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int position_id  = (NumberUtils.isNumber(posid)) ? Integer.valueOf(posid) : 0;
        int coodinate_id = (NumberUtils.isNumber(pid)) ? Integer.valueOf(pid) : 0;
        
        models.Point point  = new models.Point();
        point.setPosition_id(position_id);
        point.setCoordinate_id(coodinate_id);
        point.setType(type);
        point.setLatitude(Float.valueOf(lat));
        point.setLongtitude(Float.valueOf(lng));
        point.setUser_id(user.getId().intValue());
        point.setStr_date(sdf.format(date));
        
        return point;
	}
	
	public static void ajax_insert() {
		models.Point point = getParams();
        pointService.insertPoint(point);
        renderJSON(point);
    }
	public static void point_get() {
		String posid= params.get("position_id");
		int position_id  = (NumberUtils.isNumber(posid)) ? Integer.valueOf(posid) : 0;
		List<models.Point> point= pointService.getPoint(position_id);
        renderJSON(point);
	}
	public static void ajax_update(){
		models.Point point = getParams();
		pointService.updatePoint(point);
	}
	
	public static void ajax_remove() {
		models.Point point = getParams();
		pointService.remove(point);
	}
	
	public static void ajax_get() {
		List<models.Point> points = pointService.getAllByUsername(Secure.Security.connected());
		renderJSON(points);
	}
	
	public static void  ajax() {
		List<models.Point> points = pointService.loadPhoto(Secure.Security.connected());
		renderJSON(points);
	}
	public static void  point_photo() {
		String posid= params.get("position_id");
		int position_id  = (NumberUtils.isNumber(posid)) ? Integer.valueOf(posid) : 0;
		List<models.Point> point= pointService.point_photo(position_id);
        renderJSON(point);
	}
	public static void  insert_url() {
		String posid= params.get("posid");
		String link = params.get("link");
		String name = params.get("name");
		int position_id  = (NumberUtils.isNumber(posid)) ? Integer.valueOf(posid) : 0;
		models.Point point  = new models.Point();
        point.setPosition_id(position_id);
        point.setLink(link);
        //point.setName("test");
        point.setName(name);
		pointService.insertUrl(point);
	}
	public static void get_point() {
		List<models.Point> points = pointService.getAllByUsername(Secure.Security.connected());
		renderJSON(points);
	}
}
